//
//  DownloadCell.m
//  Hayate
//
//  Created by 韩 国翔 on 11-12-2.
//  Copyright 2011年 山东海天软件学院. All rights reserved.
//

#import "DownloadCell.h"
#import "DownLoadHelper.h"

@implementation DownloadCell
@synthesize fileInfo;
@synthesize progress;
@synthesize fileIcon;
@synthesize fileName;
@synthesize fileCurrentSize;
@synthesize fileSize;
@synthesize operateButton;
@synthesize operateIcon;
@synthesize operateText;
@synthesize request;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Load the top-level objects from the custom cell XIB.
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DownloadCell" owner:self options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        self = [topLevelObjects objectAtIndex:0];
        self.opaque = YES;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self addNewsView];
    }
    
    return self;
}

- (void)addNewsView
{
    UIImage * backgroundImage = [[UIImage imageNamed:@"progress_gray.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
    UIImage * foregroundImage = [[UIImage imageNamed:@"progress_green.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
    self.progress = [[MCProgressBarView alloc] initWithFrame:CGRectMake(70, 32, SuperWidth - 142, 10)
                                             backgroundImage:backgroundImage foregroundImage:foregroundImage];
    self.progress.progress = 0.0;
    [self.contentView addSubview:self.progress];
}

- (UIView*)superviewWithClass:(Class)class child:(UIView*)child
{
    UIView *superview = nil;
    superview = child.superview;
    while (superview != nil && ![superview isKindOfClass:class]) {
        superview = superview.superview;
    }
    return superview;
}

-(IBAction)operateTask:(id)sender
{
    UIButton *btnOperate=(UIButton *)sender;
//    FileAttributeModel *downFile=((DownloadCell *)[[[btnOperate superview] superview] superview]).fileInfo;
    FileAttributeModel *downFile = ((DownloadCell *)[self superviewWithClass:[DownloadCell class] child:btnOperate]).fileInfo;
    
    if(downFile.isDownloading)//文件正在下载，点击之后暂停下载
    {
        [operateIcon setImage:[UIImage imageNamed:@"btn_icon_download.png"]];
        [operateText setText:@"继续"];
        downFile.isDownloading=NO;
        [request cancel];
        [request clearDelegatesAndCancel];
        request=nil;
    }
    else
    {
        [operateIcon setImage:[UIImage imageNamed:@"btn_icon_paused.png"]];
        [operateText setText:@"暂停"];
        downFile.isDownloading=YES;
        
        DownLoadHelper *downLoadHelper = [DownLoadHelper sharedInstance];
        [downLoadHelper beginRequest:downFile isBeginDown:YES];
    }
    
    //暂停意味着这个Cell里的ASIHttprequest已被释放，要及时更新table的数据，使最新的ASIHttpreqst控制Cell
//    UITableView *tableView=(UITableView *)[[[[btnOperate superview] superview] superview] superview];
    UITableView *tableView =(UITableView *)[self superviewWithClass:[UITableView class] child:btnOperate];
    [tableView reloadData];
}

@end
